//Does most of the calculations for drawing -- as per this http://www.p01.org/articles/DHTML_techniques/Drawing_lines_in_JavaScript/
//dependendent on the returnImage() function that is created dynamically to return the embedded gifs for drawing

function drawLockedNodes(parentId, dependentIds, Ax, Ay, blnLockToX, blnLockToY, blnSnapBelow, parentDependencyIds, childDependencyIds) 
{ 
    //blnLockToX += 40;

    if (dependentIds != '') 
    {
        var getDependencies = dependentIds.split(','); 
        if (blnLockToX || blnLockToY)
        {
            for (d = 0; d < getDependencies.length; d++) 
            { 
                var gDependency = document.getElementById(getDependencies[d]);
                if (gDependency)
                {          
                    var objectHandle = document.getElementById(parentId + 'ParentOf' + getDependencies[d]); 
                    var Bx = gDependency.style.left; 
                    var By = gDependency.style.top;
                    Bx = parseInt(Bx.replace('px','')); By = parseInt(By.replace('px','')); 
                    if (document.getElementById(getDependencies[d] + 'InitPosition').innerHTML == '')
                    {
                        document.getElementById(getDependencies[d] + 'InitPosition').innerHTML = Bx + "," + By;  
                    }   
                    
                    if (document.getElementById(parentId + 'InitPosition').innerHTML == '')
                    {
                        var Bxx = document.getElementById(parentId).style.left; 
                        var Byx = document.getElementById(parentId).style.top;
                        Bxx = parseInt(Bxx.replace('px','')); Byx = parseInt(Byx.replace('px','')); 
                    
                        document.getElementById(parentId + 'InitPosition').innerHTML = Bxx + "," + Byx;  
                    }       
                    
                    var origCoors = document.getElementById(parentId + 'InitPosition').innerHTML.split(',');
                    var origX = parseInt(origCoors[0]);
                    var origY = parseInt(origCoors[1]);
                    
                    var origChildCoors = document.getElementById(getDependencies[d] + 'InitPosition').innerHTML.split(',');
                    var origCX = parseInt(origChildCoors[0]);
                    var origCY = parseInt(origChildCoors[1]);
                                            
                    var parX = (Ax - origX) + origCX;
                    var parY = (Ay - origY) + origCY;
                    
                    if (blnSnapBelow)
                    {
                        parX = parseInt(document.getElementById(parentId).style.left.replace('px', ''));
                        gHeight = parseInt(document.getElementById(parentId).style.height.replace('px','')) * (d + 1);
                        parY = parseInt(document.getElementById(parentId).style.top.replace('px', '')) + gHeight;
                    }
                    
                    //window.status = (Ax - origX).toString() + '|' + origX + "|" + origCX + "|" + parX;
                    
                    if (blnLockToX) {
                        gDependency.style.left = parX + "px";
                    }
                    
                    if (blnLockToY) {                        
                        gDependency.style.top = parY + "px";
                    }
                    
                    
                    if (document.getElementById(getDependencies[d] + 'DrawParentLines').innerHTML == 'true')
                    {      
                        var drawAllParentLines = parentDependencyIds.split('~');
                        var parentIDx = '';
                        
                        for(dp = 0; dp < drawAllParentLines.length; dp++)
                        {
                            if (drawAllParentLines[dp] != '')
                            {
                                var holdPLines = drawAllParentLines[dp].split(':');
                                parentIDx = holdPLines[0];
                                var getDependenciesP = holdPLines[1].split(',');
                                     
                                for (dx = 0; dx < getDependenciesP.length; dx++) 
                                {
                                    if (getDependenciesP[dx] != '')
                                    {
                                        //if (document.getElementById(parentIDx + "SnapChildrenBelow").innerHTML == 'false')
                                        //{
                                        getArrow = document.getElementById(parentIDx + 'ParentOf' + getDependenciesP[dx] + 'Arrow');
                                        objectHandle = document.getElementById(parentIDx + 'ParentOf' + getDependenciesP[dx]); 
                                        checkChild = document.getElementById(getDependenciesP[dx] + 'ChildOf' + parentIDx); 
                                        checkChildArrow = document.getElementById(getDependenciesP[dx] + 'ChildOf' + parentIDx + 'Arrow'); 
                                        if (checkChild) { checkChild.style.visibility='hidden'; }
                                        if (checkChildArrow) { checkChildArrow.style.visibility='hidden'; }
                                        
                                        if (objectHandle) 
                                        { 
                                            gDependencyP = document.getElementById(getDependenciesP[dx]);
                                            if (gDependencyP)
                                            {
                                                var getOwningDependency = document.getElementById(parentIDx);
                                                Ax = getOwningDependency.style.left; 
                                                Ay = getOwningDependency.style.top;
                                                Ax = parseInt(Ax.replace('px','')); Ay = parseInt(Ay.replace('px','')); 
                                                
                                                Bx = gDependencyP.style.left; 
                                                By = gDependencyP.style.top;
                                                Bx = parseInt(Bx.replace('px','')); By = parseInt(By.replace('px','')); 

                                                updateLine(objectHandle, Ax, Ay, Bx, By, getArrow, parentIDx, false); 
                                            }
                                        //} 
                                        }
                                    }
                                } 
                            }
                            //drawParentLine(parentIDx, drawAllParentLines[dp], Bx, By);
                        }
                    }
                    
                    if (document.getElementById(getDependencies[d] + 'DrawChildLines').innerHTML == 'true')
                    {        
                        var drawAllChildLines = childDependencyIds.split('~');
                        var childIDx = '';
                        //window.status = 'length:' + drawAllChildLines.length;
                        for(dp = 0; dp < drawAllChildLines.length; dp++)
                        {
                            if (drawAllChildLines[dp] != '')
                            {
                                var holdCLines = drawAllChildLines[dp].split(':');
                                childIDx = holdCLines[0];
                                var getDependenciesC = holdCLines[1].split(',');
                                for (dxx = 0; dxx < getDependenciesC.length; dxx++) 
                                { 
                                    if (getDependenciesC[dxx] != '')
                                    {
                                        //if (document.getElementById(getDependencies[d] + "SnapChildrenBelow").innerHTML == 'false')
                                        //{
                                            getArrow = document.getElementById(childIDx + 'ChildOf' + getDependenciesC[dxx] + 'Arrow');  
                                            objectHandle = document.getElementById(childIDx + 'ChildOf' + getDependenciesC[dxx]); 
                                            checkParent = document.getElementById(getDependenciesC[dxx] + 'ParentOf' + childIDx);
                                            checkParentArrow = document.getElementById(getDependenciesC[dxx] + 'ParentOf' + childIDx + 'Arrow'); 
                                            if (checkParent) { checkParent.style.visibility='hidden'; } 
                                            if (checkParentArrow) { checkParentArrow.style.visibility='hidden'; }

                                            if (objectHandle)
                                            { 
                                                var gDependencyC = document.getElementById(getDependenciesC[dxx]);
                                                if (gDependencyC)
                                                {
                                                    var getOwningDependency = document.getElementById(childIDx);
                                                    Bx = getOwningDependency.style.left; 
                                                    By = getOwningDependency.style.top;
                                                    Bx = parseInt(Bx.replace('px','')); By = parseInt(By.replace('px','')); 
                                                
                                                    Ax = gDependencyC.style.left;
                                                    Ay = gDependencyC.style.top;                 
                                                    Ax = parseInt(Ax.replace('px','')); Ay = parseInt(Ay.replace('px','')); 
                                                    
                                                    updateLine(objectHandle, Ax, Ay, Bx, By, getArrow, childIDx, true); 
                                                }
                                            }
                                        //} 
                                    }
                                    //drawChildLine(childIDx, drawAllChildLines[dp], Bx, By);
                                }
                            }
                        }
                    }                   
                }
            }
        }
    } 
} 

function drawParentLine(parentId, dependentIds, Ax, Ay) 
{ 
    if (dependentIds != '') 
    { 
        var getDependencies = dependentIds.split(','); 
        for (d = 0; d < getDependencies.length; d++) 
        { 
            var getArrow = document.getElementById(parentId + 'ParentOf' + getDependencies[d] + 'Arrow');
            var objectHandle = document.getElementById(parentId + 'ParentOf' + getDependencies[d]); 
            var checkChild = document.getElementById(getDependencies[d] + 'ChildOf' + parentId); 
            var checkChildArrow = document.getElementById(getDependencies[d] + 'ChildOf' + parentId + 'Arrow'); 
            if (checkChild) { checkChild.style.visibility='hidden'; }
            if (checkChildArrow) { checkChildArrow.style.visibility='hidden'; }
            
            if (objectHandle) 
            { 
                var gDependency = document.getElementById(getDependencies[d]);
                if (gDependency)
                {
                    
                    var Bx = gDependency.style.left; 
                    var By = gDependency.style.top;
                    Bx = parseInt(Bx.replace('px','')); By = parseInt(By.replace('px','')); 

                    updateLine(objectHandle, Ax, Ay, Bx, By, getArrow, parentId, false); 
                }
            } 
        } 
    } 
} 

function drawChildLine(childId, dependentIds, Bx, By) 
{ 
    if (dependentIds != '') 
    { 
        var getDependencies = dependentIds.split(','); 
        for (d = 0; d < getDependencies.length; d++) 
        { 
            var getArrow = document.getElementById(childId + 'ChildOf' + getDependencies[d] + 'Arrow');  
            var objectHandle = document.getElementById(childId + 'ChildOf' + getDependencies[d]); 
            var checkParent = document.getElementById(getDependencies[d] + 'ParentOf' + childId);
            var checkParentArrow = document.getElementById(getDependencies[d] + 'ParentOf' + childId + 'Arrow'); 
            if (checkParent) { checkParent.style.visibility='hidden'; } 
            if (checkParentArrow) { checkParentArrow.style.visibility='hidden'; }

            if (objectHandle)
            { 
                var gDependency = document.getElementById(getDependencies[d]);
                if (gDependency)
                {
                    var Ax = gDependency.style.left;
                    var Ay = gDependency.style.top;                 
                    Ax = parseInt(Ax.replace('px','')); Ay = parseInt(Ay.replace('px','')); 
                    
                    updateLine(objectHandle, Ax, Ay, Bx, By, getArrow, childId, true); 
                }
            } 
        } 
    } 
} 


//window.onerror = handleErr; 
//function handleErr(msg,url,l) 
//{ 
//    window.status = msg + ' | ' + url + ' | ' + l; 
//    return true
//}

function updateLine(objectHandle, Ax, Ay, Bx, By, arrowHandle, nodeId, isChild) 
{
    objectHandle.style.visibility='visible'; 
    arrowHandle.style.visibility='visible';
    
    /*if(Bx>Ax)
    {
        Bx=Bx+30;
        By=By+30;
    }
    else
    {
        Bx=Bx-10;
        By=By+30;
    }*/
    
    Ax=Ax+37;
    Ay=Ay+37;
    
    By = By + 23;
    Bx = Bx + 2;
       
    /*
    if (isChild) {
        if (Ax - Bx > 0) {
             Bx = Bx + parseInt(document.getElementById(nodeId).style.width.replace('px',''));
        }
    } else {
        if (Ax - Bx > 0) {
            Ax = Ax + parseInt(document.getElementById(nodeId).style.width.replace('px',''));
        }
    }
    */
    
    //window.status = lf;
    
    var
    xMin		= Math.min( Ax, Bx ),
    yMin		= Math.min( Ay, By ),
    xMax		= Math.max( Ax, Bx ),
    yMax		= Math.max( Ay, By ),
    boxWidth	= Math.max( xMax-xMin, 1 ),
    boxHeight	= Math.max( yMax-yMin, 1 ),
    tmp		= Math.min( boxWidth, boxHeight ),
    smallEdge	= 1,
    newSrc
    
    //window.status = ' 1st tmp: ' + boxWidth + ',' + boxHeight + ',' + tmp;
    
    while(tmp > 200)
        tmp--;
        
    while (tmp % 10 != 0 && tmp > 10)
        tmp--;
        
    if (tmp <= 0)
        tmp = 1;
    
    //while( tmp>>=1 )
    //smallEdge<<=1
    
    smallEdge = tmp;
    
    var dir = '';
    
    //window.status = window.status + ' smallEdge: ' + smallEdge;
    
   

      
    if ( (Bx - Ax) * (By - Ay) < 0) { dir = 'up'; } else { dir = 'down'; } 
    
    var nData = document.getElementById(nodeId + 'Data');
    var lType = '', lDeco = '', lColor = '', pDeco = '', lWidth = 1;
    var bBot, bTop, bRight, bLeft;
    if (nData) { gdata = nData.innerHTML.split('|'); lType = gdata[0]; lDeco = gdata[1]; lColor = gdata[2]; lWidth = gdata[3]; }

    if (lDeco == '0') {
        pDeco = "solid";
    } else if (lDeco == "1") { 
        pDeco = "dotted";
    }

    if (lType == 'direct') {
        newSrc = returnImage(smallEdge, dir, lColor, lDeco, lWidth);
        bTop = 'none'; bBot = 'none'; bRight = 'none'; bLeft = 'none';
    } else {
        newSrc = returnTransparentPixel();

        if (Ay - By > 0) {
            bBot = lWidth + "px " + pDeco + " #" + lColor;
            bTop = "none";
        } else {
            bTop = lWidth + "px " + pDeco + " #" + lColor;
            bBot = "none";
        }
        
        if (Ax - Bx > 0) {
            bLeft = lWidth + "px " + pDeco + " #" + lColor;
            bRight = "none";
        } else {
            bRight = lWidth + "px " + pDeco + " #" + lColor;
            bLeft = "none";
        }
    }
    
    if( objectHandle.src.indexOf( newSrc )== -1 )
        objectHandle.src = newSrc

    with( objectHandle.style )
    {
        //window.status = window.status +  ' Ax: ' + Ax + ' | Ay: ' + Ay + ' | Bx: ' + Bx + ' By: ' + By + ' | bWidth: ' + boxWidth + ' | bHeight: ' + boxHeight + ' xMin: ' + xMin + ' | yMin: ' + yMin; 
        width	= boxWidth	+ "px" 
        height	= boxHeight	+ "px" 
        left	= xMin		+ "px" 
        top	= yMin	+ "px"
        borderLeft = bLeft
        borderRight = bRight
        borderTop = bTop
        borderBottom = bBot
    }
    
    objectHandle.src = "../images/imgtrans.png";
    
    arrowDir = '';
    if (Ay - By > 0) {
        arrowDir = 'up';
    } else {
        arrowDir = 'down';
    }
    
    arrowHandle.src = returnArrow(smallEdge, arrowDir, lColor, lDeco, lWidth);   
    
    yArrowOffset = By;
    if (arrowDir == 'up') {
        yArrowOffset = By;
    } else {
        yArrowOffset = By;
    }
    
    xArrowOffset = -7;
    if (lWidth > 2)
        xArrowOffset = xArrowOffset + (lWidth/1.3);
    
    with ( arrowHandle.style )
    {
        top = yArrowOffset + "px";
        left = (Bx - xArrowOffset) + "px";
    }
    
    arrowHandle.src = "../images/imgtrans.png";
} 

function MoveNode(parentId, dependentParentIds, dependentChildIds, XDif, YDif)
{
    //move the div itself
    document.getElementById(parentId).style.left = (parseInt(document.getElementById(parentId).style.left.replace('px','')) + XDif) + 'px';
    document.getElementById(parentId).style.top = (parseInt(document.getElementById(parentId).style.top.replace('px','')) + YDif) + 'px';
    
    /*        
    //move the dependencies
    var getDependencies = dependentParentIds.split(','); 
    for (d = 0; d < getDependencies.length; d++) 
    { 
        //move lines and arrows this node is parent to
        var checkChild = document.getElementById(parentId + 'ChildOf' + getDependencies[d]); 
        var checkChildArrow = document.getElementById(parentId + 'ChildOf' + getDependencies[d] + 'Arrow');
        
        if (checkChild) { checkChild.style.top = (parseInt(checkChild.style.top.replace('px','')) + YDif) + "px";
                           checkChild.style.left = (parseInt(checkChild.style.left.replace('px','')) + XDif) + "px"; }
        
        if (checkChildArrow) { checkChildArrow.style.top = (parseInt(checkChildArrow.style.top.replace('px','')) + YDif) + "px";
                                checkChildArrow.style.left = (parseInt(checkChildArrow.style.left.replace('px','')) + XDif) + "px"; }
                                
        //move child
        var checkChild = document.getElementById(getDependencies[d] + 'ParentOf' + parentId); 
        var checkChildArrow = document.getElementById(getDependencies[d] + 'ParentOf' + parentId + 'Arrow'); 
        if (checkChild) { checkChild.style.top = (parseInt(checkChild.style.top.replace('px','')) + YDif) + "px";
                           checkChild.style.left = (parseInt(checkChild.style.left.replace('px','')) + XDif) + "px"; } 
                           
        if (checkChildArrow) { checkChildArrow.style.top = (parseInt(checkChildArrow.style.top.replace('px','')) + YDif) + "px";
                                checkChildArrow.style.left = (parseInt(checkChildArrow.style.left.replace('px','')) + XDif) + "px"; }
    }
    
    getDependencies = dependentChildIds.split(','); 
    for (d = 0; d < getDependencies.length; d++) 
    { 
        //move lines and arrows this node is parent to
        var checkParent = document.getElementById(parentId + 'ParentOf' + getDependencies[d]); 
        var checkParentArrow = document.getElementById(parentId + 'ParentOf' + getDependencies[d] + 'Arrow');
        
        if (checkParent) { checkParent.style.top = (parseInt(checkParent.style.top.replace('px','')) + YDif) + "px";
                           checkParent.style.left = (parseInt(checkParent.style.left.replace('px','')) + XDif) + "px"; }
        
        if (checkParentArrow) { checkParentArrow.style.top = (parseInt(checkParentArrow.style.top.replace('px','')) + YDif) + "px";
                                checkParentArrow.style.left = (parseInt(checkParentArrow.style.left.replace('px','')) + XDif) + "px"; }
                                
        //move child
        var checkChild = document.getElementById(getDependencies[d] + 'ChildOf' + parentId); 
        var checkChildArrow = document.getElementById(getDependencies[d] + 'ChildOf' + parentId + 'Arrow'); 
        if (checkChild) { checkChild.style.top = (parseInt(checkChild.style.top.replace('px','')) + YDif) + "px";
                           checkChild.style.left = (parseInt(checkChild.style.left.replace('px','')) + XDif) + "px"; } 
                           
        if (checkChildArrow) { checkChildArrow.style.top = (parseInt(checkChildArrow.style.top.replace('px','')) + YDif) + "px";
                                checkChildArrow.style.left = (parseInt(checkChildArrow.style.left.replace('px','')) + XDif) + "px"; }
                                
    }
    */

}